//
//  DYADViewController.m
//  BaiSi
//
//  Created by mac on 16/10/17.
//  Copyright © 2016年 DeYi. All rights reserved.
//

#import "DYADViewController.h"
#import <AFNetworking/AFNetworking.h>
#import <MJExtension/MJExtension.h>
#import <SDWebImage/UIImageView+WebCache.h>

#import "DYTabBarController.h"


#import "DYADItem.h"


#define ADParmeters @"phcqnauGuHYkFMRquANhmgN_IauBThfqmgKsUARhIWdGULPxnz3vndtkQW08nau_I1Y1P1Rhmhwz5Hb8nBuL5HDknWRhTA_qmvqVQhGGUhI_py4MQhF1TvChmgKY5H6hmyPW5RFRHzuET1dGULnhuAN85HchUy7s5HDhIywGujY3P1n3mWb1PvDLnvF-Pyf4mHR4nyRvmWPBmhwBPjcLPyfsPHT3uWm4FMPLpHYkFh7sTA-b5yRzPj6sPvRdFhPdTWYsFMKzuykEmyfqnauGuAu95Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiu9mLfqHbD_H70hTv6qnHn1PauVmynqnjclnj0lnj0lnj0lnj0lnj0hThYqniuVujYkFhkC5HRvnB3dFh7spyfqnW0srj64nBu9TjYsFMub5HDhTZFEujdzTLK_mgPCFMP85Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiuBnHfdnjD4rjnvPWYkFh7sTZu-TWY1QW68nBuWUHYdnHchIAYqPHDzFhqsmyPGIZbqniuYThuYTjd1uAVxnz3vnzu9IjYzFh6qP1RsFMws5y-fpAq8uHT_nBuYmycqnau1IjYkPjRsnHb3n1mvnHDkQWD4niuVmybqniu1uy3qwD-HQDFKHakHHNn_HR7fQ7uDQ7PcHzkHiR3_RYqNQD7jfzkPiRn_wdKHQDP5HikPfRb_fNc_NbwPQDdRHzkDiNchTvwW5HnvPj0zQWndnHRvnBsdPWb4ri3kPW0kPHmhmLnqPH6LP1ndm1-WPyDvnHKBrAw9nju9PHIhmH9WmH6zrjRhTv7_5iu85HDhTvd15HDhTLTqP1RsFh4ETjYYPW0sPzuVuyYqn1mYnjc8nWbvrjTdQjRvrHb4QWDvnjDdPBuk5yRzPj6sPvRdgvPsTBu_my4bTvP9TARqnam"


@interface DYADViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *ADImageView;

@property (weak, nonatomic) IBOutlet UIView *BackgroundView;
@property (weak, nonatomic) IBOutlet UIButton *JumpLabel;

@property(nonatomic,strong) DYADItem *item;

@property(nonatomic,weak) NSTimer *timer;

@end

@implementation DYADViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view from its nib.
    
    //设置背景图片
    [self setUpBackgroundView];
    
    //加载广告数据
    [self addADData];
    
    //添加定时器
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerChange) userInfo:nil repeats:YES];
}

#pragma mark - 定时器倒计时
- (void)timerChange {
    static int time = 3;
    
        time--;
    [_JumpLabel setTitle:[NSString stringWithFormat:@"跳过( %d秒 )", time] forState:UIControlStateNormal] ;
   if (time == -1) {
        
       [self JumpAD:nil];
   
    }
 
   
}

#pragma mark - 加载广告数据
-(void)addADData{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"code2"] = ADParmeters;
    
    [manager GET:@"http://mobads.baidu.com/cpro/ui/mads.php" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //解析数据
        NSMutableDictionary *response = responseObject;
       [response writeToFile:@"/Users/mac/Desktop/BaiSi/AD.plist" atomically:YES];
        response =  [response[@"ad"] firstObject];
        DYADItem *item = [DYADItem mj_objectWithKeyValues:response];
        _item = item;
        
        //判断广告图片是否存在,否则,跳过.
        if (!_item.w) {
            return ;
        }
        //加载图片
        UIImageView *adView = [[UIImageView alloc] init];
        [self.BackgroundView addSubview:adView];
        CGFloat w = DYScreenW;
        CGFloat h = w / item.w * item.h;
        
        if(h > DYScreenH * 0.8) {
            h = DYScreenH * 0.8;
        }
        adView.frame = CGRectMake(0, 0, w, h);
        [adView sd_setImageWithURL:[NSURL URLWithString:item.w_picurl]];
        
        //添加点按手势,进入广告.
        adView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(JumpToAD)];
        [adView addGestureRecognizer:tap];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        LHLog(@"%@",error);
        
    }];
    
    
}

#pragma mark - 进入广告
- (void)JumpToAD {
    
    NSURL *url = [NSURL URLWithString:_item.ori_curl];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
    }
    
    LHLog(@"点击广告");
    
}

#pragma mark - 点击跳过广告
- (IBAction)JumpAD:(id)sender {
    
    LHLog(@"点击跳过广告");
    //创建tabBar控制器
    DYTabBarController *tab = [[DYTabBarController alloc]init];
    [UIApplication sharedApplication].keyWindow.rootViewController = tab;
    
    [_timer invalidate];
}



#pragma mark - 设置背景图片
- (void)setUpBackgroundView {
    //屏幕适配
    if (iPhone4) {
        _ADImageView.image = [UIImage imageNamed:@"LaunchImage"];
        
    } else if (iPhone5) {
        _ADImageView.image = [UIImage imageNamed:@"LaunchImage-568h"];
        
    } else if (iPhone6) {
        _ADImageView.image = [UIImage imageNamed:@"LaunchImage-800-667h"];
        
    } else if (iPhone6p) {
        _ADImageView.image = [UIImage imageNamed:@"LaunchImage-800-Portrait-736h@3x"];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
} 

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
